package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.product.domain.InsBottleType;
import com.ruoyi.project.product.service.IInsBottleTypeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 包装瓶类型Controller
 *
 * @author taney
 * @date 2020-11-17
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/bottleType")
public class InsBottleTypeController extends BaseController {

    private final IInsBottleTypeService iInsBottleTypeService;

    /**
     * 查询包装瓶类型列表
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsBottleType insBottleType) {
        startPage();
        LambdaQueryWrapper<InsBottleType> lqw = new LambdaQueryWrapper<InsBottleType>();
        if (StringUtils.isNotBlank(insBottleType.getMachineType())) {
            lqw.eq(InsBottleType::getMachineType, insBottleType.getMachineType());
        }
        if (insBottleType.getSerialNo() != null) {
            lqw.like(InsBottleType::getSerialNo, insBottleType.getSerialNo());
        }
        if (StringUtils.isNotBlank(insBottleType.getBottleName())) {
            lqw.like(InsBottleType::getBottleName, insBottleType.getBottleName());
        }
        List<InsBottleType> list = iInsBottleTypeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出包装瓶类型列表
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:export')")
    @Log(title = "包装瓶类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsBottleType insBottleType) {
        LambdaQueryWrapper<InsBottleType> lqw = new LambdaQueryWrapper<InsBottleType>(insBottleType);
        List<InsBottleType> list = iInsBottleTypeService.list(lqw);
        ExcelUtil<InsBottleType> util = new ExcelUtil<InsBottleType>(InsBottleType.class);
        return util.exportExcel(list, "bottleType");
    }

    /**
     * 获取包装瓶类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:query')")
    @GetMapping(value = "/{bottleTypeId}")
    public AjaxResult getInfo(@PathVariable("bottleTypeId") Integer bottleTypeId) {
        return AjaxResult.success(iInsBottleTypeService.getById(bottleTypeId));
    }

    /**
     * 新增包装瓶类型
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:add')")
    @Log(title = "包装瓶类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsBottleType insBottleType) {
        return toAjax(iInsBottleTypeService.save(insBottleType) ? 1 : 0);
    }

    /**
     * 修改包装瓶类型
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:edit')")
    @Log(title = "包装瓶类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsBottleType insBottleType) {
        return toAjax(iInsBottleTypeService.updateById(insBottleType) ? 1 : 0);
    }

    /**
     * 删除包装瓶类型
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:remove')")
    @Log(title = "包装瓶类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bottleTypeIds}")
    public AjaxResult remove(@PathVariable Integer[] bottleTypeIds) {
        return toAjax(iInsBottleTypeService.removeByIds(Arrays.asList(bottleTypeIds)) ? 1 : 0);
    }


    /**
     * 根据机型查询包装瓶类型列表
     */
    @PreAuthorize("@ss.hasPermi('product:bottleType:listByMachineType')")
    @GetMapping("/listByMachineType/{machineType}")
    public AjaxResult listByMachineType(@PathVariable String machineType) {
        LambdaQueryWrapper<InsBottleType> lqw = new LambdaQueryWrapper<InsBottleType>();
        lqw.eq(InsBottleType::getMachineType, machineType);
        List<InsBottleType> list = iInsBottleTypeService.list(lqw);
        return AjaxResult.success(list);
    }
}
